<script src="#(CPATH)/static/components/ckeditor4/ckeditor.js"></script>
<script>

    function openlayer(event) {
        layer.data.src = null;
        layer.open({
            type: 2,
            title: '选择图片',
            anim: 2,
            shadeClose: true,
            shade: 0.5,
            area: ['90%', '90%'],
            content: jpress.cpath + '/admin/attachment/browse',
            end: function () {
                if (layer.data.src != null) {
                    var src = layer.data.src;

                    var infoTxtUrlEle = event.data.getContentElement('info', 'txtUrl');
                    if (infoTxtUrlEle) infoTxtUrlEle.setValue(src);

                    var infoUrlEle = event.data.getContentElement('info', 'url');
                    if (infoUrlEle) infoUrlEle.setValue(src);

                    var infoSrcEle = event.data.getContentElement('info', 'src');
                    if (infoSrcEle) infoSrcEle.setValue(src);

                    var linkTxtUrlEle = event.data.getContentElement('Link', 'txtUrl')
                    if (linkTxtUrlEle) linkTxtUrlEle.setValue(src);
                }
            }
        });
    }


    function initCKEdtior4(editor, height) {
        CKEDITOR.config.toolbar =
            [

                ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
                ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat'],
                ['TextColor', 'BGColor'],
                ['Blockquote', 'CodeSnippet', 'Image', 'Html5audio', 'Html5video', 'Flash', 'Table', 'HorizontalRule'],
                ['Link', 'Unlink', 'Anchor'],
                ['Outdent', 'Indent'],
                ['NumberedList', 'BulletedList'],

                '/',

                ['Undo', 'Redo'],
                ['Format', 'FontSize'],
                ['Maximize', 'Source']
            ];

        CKEDITOR.config.allowedContent = true;
        CKEDITOR.config.extraAllowedContent = '*(*);*{*}';
        CKEDITOR.config.wordcount = {
            showCharCount: true,
        };

        CKEDITOR.config.disallowedContent = 'img{width,height};img[width,height]';
        CKEDITOR.addCss('.cke_editable img{max-width: 95%;}');

        var ed = CKEDITOR.replace(editor, {
            autoUpdateElement: true,
            removePlugins: 'easyimage,cloudservices',
            extraPlugins: 'entities,codesnippet,uploadimage,image,notification,widget,widgetselection,clipboard,lineutils',
            codeSnippet_theme: 'monokai_sublime',
            height: height,
            uploadUrl: jpress.cpath + '/commons/ckeditor/upload',
            imageUploadUrl: jpress.cpath + '/commons/ckeditor/upload',
            filebrowserUploadUrl: jpress.cpath + '/commons/ckeditor/upload',
            filebrowserBrowseUrl: jpress.cpath + '/admin/attachment/browse',
            language: 'zh-cn'
        });


        ed.on('instanceReady', function () {
            ed.setKeystroke(CKEDITOR.ALT.CTRL + 83, 'save'); //  Ctrl+s
            ed.setKeystroke(1114195, 'save'); // mac command +s
            // 扩展CKEditor的 ctrl + s 保存命令,方便全屏编辑时快捷保存
            ed.addCommand('save', {
                exec: function () {
                    var ds = window.doSubmit;
                    ds && ds();
                }
            });
        });


        ed.on("dialogShow", function (event) {
            // 方便调试
            _dialogShowEvent = event;

            var infoEle = event.data.getContentElement("info", "browse");
            if (infoEle) infoEle.removeAllListeners();

            var linkEle = event.data.getContentElement("Link", "browse");
            if (linkEle) linkEle.removeAllListeners();

            $(".cke_dialog_ui_button").each(function () {
                if ("浏览服务器" == $(this).text()) {
                    $(this).off("click");
                    $(this).on("click", function (e) {
                        e.stopPropagation();
                        openlayer(event);
                        return false;
                    })
                } else {
                    $(this).off("click");
                }
            })

        });

        return ed;
    }

    initCKEdtior4('editor', 460);


    window.insertImageToEditor = function (src,title){
        var img = document.createElement('img')
        img.setAttribute('src', src)
        img.setAttribute('alt', title)
        insertHtmlToEditor(img.outerHTML);
    }


    window.insertVideoToEditor = function (src,title){
        var video = document.createElement('video')
        video.setAttribute('src', src)
        video.setAttribute('style', 'max-width:100%;')
        video.setAttribute('controls', 'controls')

        insertHtmlToEditor(video.outerHTML);
    }


    window.insertAttachementToEditor = function (src,title){
        var a = document.createElement('a')
        a.setAttribute('href', src)
        a.setAttribute("target", "_blank")
        a.innerHTML = title;
        insertHtmlToEditor('<p class="attachment-item">附件：' + a.outerHTML+"</p>");
    }


    window.insertHtmlToEditor = function (html){
        CKEDITOR.instances['editor'].insertHtml(html);
    }

    window.onEditorSubmitBefore = function (){

    }

</script>